package com.adapters;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.activities.ViewSecurityPackageActivity;
import com.objects.OutboxObject;
import com.senditcertified.mail.R;

public class OutboxAdapter1 extends BaseExpandableListAdapter {
	private Context myContext;
	private List<List<OutboxObject>> myData;

	public OutboxAdapter1(Context context, List<List<OutboxObject>> data) {
		myContext = context;
		myData = data;
	}

	@Override
	public OutboxObject getChild(int groupPosition, int childPosition) {
		OutboxObject item = null;
		try {
			item = getGroup(groupPosition).get(childPosition);
		} catch (Exception ex) {
		}
		return item;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolderItem holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(this.myContext).inflate(
					R.layout.outbox_item_child1, parent, false);
			holder = new ViewHolderItem();
			convertView.setTag(holder);
		} else {
			holder = (ViewHolderItem) convertView.getTag();
		}
		
		return convertView;
	}
	
	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return getGroup(groupPosition).size();
	}

	@Override
	public List<OutboxObject> getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return myData.get(groupPosition);
	}

	public OutboxObject getGroupName(int groupPosition) {
		return myData.get(groupPosition).get(0);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return myData.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		OutboxObject headerTitle = getGroupName(groupPosition);
		ViewHolderGroup holder = new ViewHolderGroup();
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) this.myContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.out_box_item_group1,
					parent, false);
			holder.imgAdd = (ImageView) convertView.findViewById(R.id.img_add);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolderGroup) convertView.getTag();
		}

		if (isExpanded) {
			holder.imgAdd.setImageDrawable(myContext.getResources()
					.getDrawable(R.drawable.outbox_ic_sub_item));
		} else {
			holder.imgAdd.setImageDrawable(myContext.getResources()
					.getDrawable(R.drawable.outbox_ic_add));
		}

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	private static class ViewHolderGroup {
		TextView tvDate;
		TextView tvHours;
		TextView tvContent;
		ImageView imgAdd;
		ImageView attachment;
	}

	private static class ViewHolderItem {
		ImageView imvStatus;
		TextView tvMail;
		View viewTop;
	}
}
